package com.bwie.chenzihang.service;
import android.app.ProgressDialog;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.SystemClock;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.bwie.chenzihang.R;


public class Service4Activity extends AppCompatActivity implements View.OnClickListener {

    private EditText et_username;
    private EditText et_pwd;
    private Button btn_login;

    private Service4.MyBind myBind;

    private ServiceConnection connection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            myBind = (Service4.MyBind) service;
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    };
    private boolean isLogin = false;
    private Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(@NonNull Message msg) {
            if (isLogin) {
                Toast.makeText(Service4Activity.this, "登录成功", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(Service4Activity.this, "登录失败", Toast.LENGTH_SHORT).show();
            }
            return false;
        }
    });

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_service4);
        initView();
        new UserDB2(this).getWritableDatabase();

    }

    private void initView() {
        et_username = (EditText) findViewById(R.id.et_username);
        et_pwd = (EditText) findViewById(R.id.et_pwd);
        btn_login = (Button) findViewById(R.id.btn_login);

        btn_login.setOnClickListener(this);
        Intent intent = new Intent(this, Service4.class);
        startService(intent);
        bindService(intent, connection, BIND_AUTO_CREATE);
    }

    ProgressDialog dialog;

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_login:
                dialog = new ProgressDialog(this);
                dialog.setTitle("正在登录...");
                dialog.setCancelable(false);
                dialog.show();
                submit();
                break;
        }
    }

    private void submit() {
        new Thread() {
            @Override
            public void run() {
                String username = et_username.getText().toString().trim();
                String pwd = et_pwd.getText().toString().trim();
                SystemClock.sleep(3000);
                boolean b = myBind.setText(username, pwd);
                isLogin = b;
                dialog.dismiss();
                handler.sendEmptyMessage(1);
            }
        }.start();

    }

}